package leetcode.D400.T304;

/**
 * @File Info: leetcode -- <NumMatrix>
 * @Author: 18362
 * @Create: 2022-02-16 22:33:44 星期三
 */

class NumMatrix {

    private int[][] sumMatrix;

    public NumMatrix(int[][] matrix) {
        sumMatrix = new int[matrix.length+1][matrix[0].length+1];
        for(int i=0; i<matrix.length; ++i) {
            for(int j=0; j<matrix[0].length; ++j) {
                sumMatrix[i+1][j+1] = sumMatrix[i+1][j] + sumMatrix[i][j+1] - sumMatrix[i][j] + matrix[i][j];
            }
        }

    }

    public int sumRegion(int row1, int col1, int row2, int col2) {
        return sumMatrix[row2+1][col2+1] + sumMatrix[row1][col1] - sumMatrix[row2+1][col1] - sumMatrix[row1][col2+1];
    }
}

/**
 * Your NumMatrix object will be instantiated and called as such:
 * NumMatrix obj = new NumMatrix(matrix);
 * int param_1 = obj.sumRegion(row1,col1,row2,col2);
 */
